package top.gcl.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.gcl.model.User;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AxiosCtrl {

    @GetMapping("/getUser/{id}")
    @CrossOrigin//跨域注解
    public Map getUser(@PathVariable String id ){

        System.out.println("id: "+id);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","success");
        return map;
    }

    @PostMapping("/reg")
    @CrossOrigin(allowCredentials = "true")
    //加上allowCredentials 是让他带上cookie 不加  不好用。
    //跨域注解 可以直接加到类上，之后新建的contller都需要加上这个注解。
    public Map reg(@RequestBody User user ,HttpSession session){
        System.out.println("-session-"+session.getId());//打印session id 看看cookie中的jsessionid
        String username=(String)session.getAttribute("username");
        if(username==null||"".equals(username))
        {
            session.setAttribute("username","lilei");
        }

        System.out.println(String.format("username: %s , password: %s",user.getUsername(),user.getPassword()));
        Map res=new HashMap();
        res.put("code",0);
        res.put("msg","success");
        return res;
    }
}
